package com.gdut.huayan.utils;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.gdut.huayan.entity.vo.SmsReserveVo;

import java.util.Date;

public class AliyunUtils {

    private static final String endpoint = "https://oss-cn-beijing.aliyuncs.com";

    private static final String bucketName = "huayan-temple3";

    private static final String accessKeyId = "LTAI5t9anzGZSs8iEQ2STX26";

    private static final String accessKeySecret = "DPogDUym8WoI7otfAkxCTATxupE3do";

    private static final OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

    public static String getUploadLink(String filePath, String contentType, long expireTime) {
        if (StringUtils.isEmpty()) {
            return null;
        }
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, filePath);
        generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
        Date expiration = new Date(expireTime);
        generatePresignedUrlRequest.setExpiration(expiration);
        generatePresignedUrlRequest.setContentType(contentType);
        return ossClient.generatePresignedUrl(generatePresignedUrlRequest).toString();
    }

    public static String getUploadLink(String filePath, long expireTime) {
        if (StringUtils.isEmpty(filePath)) {
            return null;
        }
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, filePath);
        generatePresignedUrlRequest.setMethod(HttpMethod.PUT);
        Date expiration = new Date(expireTime);
        generatePresignedUrlRequest.setExpiration(expiration);
        return ossClient.generatePresignedUrl(generatePresignedUrlRequest).toString();
    }

    public static String getDownloadLink(String filePath, long expireTime) {
        if (StringUtils.isEmpty(filePath)) {
            return null;
        }
        GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, filePath);
        generatePresignedUrlRequest.setMethod(HttpMethod.GET);
        Date expiration = new Date(expireTime);
        generatePresignedUrlRequest.setExpiration(expiration);
        return ossClient.generatePresignedUrl(generatePresignedUrlRequest).toString();
    }

    public static void deleteFile(String filePath) {
        if (StringUtils.isNotEmpty(filePath)) {
            ossClient.deleteObject(bucketName, filePath);
        }
    }


    public static boolean fileIsExist(String filePath){
        return ossClient.doesObjectExist(bucketName, filePath);
    }

    /**
     * 发送短信
     * @param phoneNumber 电话号码，如果是中国大陆，country=null，135xxx，如不是，852135xxx
     * @param signName 审核通过的签名，如【华严寺】与templateCode匹配
     * @param templateCode 如SMS_268485242（验证码）、SMS_268550318（预约成功）、验证码二哥SMS_268550318
     * @param templateParam 如通知：SmsReserveVo，传json
     * @return 空为发生错误，否则返回request_id
     */
    public static String sendSMS(String phoneNumber, String signName, String templateCode, String templateParam) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        // 创建API请求并设置参数
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phoneNumber);
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setTemplateParam(templateParam);
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            return response.getRequestId();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            // 打印错误码
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return null;
    }


}